package me.flyray.crm.core.biz;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.dromara.hmily.annotation.Hmily;
import org.dromara.hmily.annotation.PatternEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.aliyuncs.utils.StringUtils;
import com.github.wxiaoqi.security.auth.client.annotation.IgnoreUserToken;
import com.github.wxiaoqi.security.auth.common.util.SnowFlake;
import com.github.wxiaoqi.security.biz.common.crm.request.FreezeRequest;
import com.github.wxiaoqi.security.biz.common.crm.request.UnFreezeRequest;
import com.github.wxiaoqi.security.biz.common.msg.BizResponseCode;
import com.github.wxiaoqi.security.common.enums.AccountStateEnums;
import com.github.wxiaoqi.security.common.enums.CustomerTypeEnums;
import com.github.wxiaoqi.security.common.enums.FreezeStateEnums;
import com.github.wxiaoqi.security.common.exception.BusinessException;
import com.github.wxiaoqi.security.common.msg.BaseApiResponse;
import com.github.wxiaoqi.security.common.util.CurrencyUtils;
import com.github.wxiaoqi.security.common.util.EntityUtils;
import com.github.wxiaoqi.security.common.util.MD5;

import me.flyray.crm.core.entity.FreezeJournal;
import me.flyray.crm.core.entity.MerchantAccount;
import me.flyray.crm.core.entity.PersonalAccount;
import me.flyray.crm.core.entity.UnfreezeJournal;
import me.flyray.crm.core.mapper.FreezeJournalMapper;
import me.flyray.crm.core.mapper.MerchantAccountMapper;
import me.flyray.crm.core.mapper.PersonalAccountMapper;
import me.flyray.crm.core.mapper.UnfreezeJournalMapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 冻结、解冻相关的接口
 */
@Service
public class CommonFreezeUnFreezeBizTcc {
	
	private static final Logger log = LoggerFactory.getLogger(MerchantPayForAnotherBankBiz.class);
	@Autowired
	private MerchantAccountMapper merchantAccountMapper;
	@Autowired
	private FreezeJournalMapper freezeJournalMapper;
	@Autowired
	private UnfreezeJournalMapper unfreezeJournalMapper;
	@Autowired
	private PersonalAccountMapper personalAccountMapper;
	@Value("${bountyHunter.balanceSaltValue}")
	private String balanceSaltValue;
	
	/**
	 * 冻结接口接口 try
	 * 若是商户 
	 * 1.商户账户余额扣除，冻结金额增加
	 * 若是个人
	 * 1.个人账户余额扣除，冻结金额增加
	 */
	@Hmily(confirmMethod="freezeConfirm",cancelMethod="freezeCancel")
	public BaseApiResponse freeze(FreezeRequest freezeRequest) {
		log.info("try 冻结接口-请求参数：{}" + EntityUtils.beanToMap(freezeRequest));
		BaseApiResponse response = new BaseApiResponse();		
		if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
			MerchantAccount queryMerchantAccount=new MerchantAccount();
			queryMerchantAccount.setPlatformId(freezeRequest.getPlatformId());
			queryMerchantAccount.setMerchantId(freezeRequest.getMerchantId());
			queryMerchantAccount.setMerchantType(freezeRequest.getMerchantType());
			queryMerchantAccount.setAccountType(freezeRequest.getAccountType());
			MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
			if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
				if(!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())){
					response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
					response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
					log.info("try 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				//校验商户账户余额是否充足 和余额加密值的校验
				if(merchantAccount.getAccountBalance().compareTo(new BigDecimal(freezeRequest.getFreezeAmt()))<0){//商户账户余额不足
					response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
					response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
					log.info("try 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				merchantAccount.setAccountBalance(merchantAccount.getAccountBalance().subtract(new BigDecimal(freezeRequest.getFreezeAmt())));
				merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance().add(new BigDecimal(freezeRequest.getFreezeAmt())));
				merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
			}else{
				log.info("try 冻结接口--商户账户不存在或者被冻结");
				response.setCode(  BizResponseCode.MER_ACC_NOTEXIST.getCode());
				response.setMessage(BizResponseCode.MER_ACC_NOTEXIST.getMessage());
				log.info("try 冻结接口 返回参数。。。。。。{}", response);
				return response;
			}
			
		}
		if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
			PersonalAccount queryPersonalAccount = new PersonalAccount();
			queryPersonalAccount.setPlatformId(freezeRequest.getPlatformId());
			queryPersonalAccount.setPersonalId(freezeRequest.getPersonalId());
			queryPersonalAccount.setAccountType(freezeRequest.getAccountType());
			PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
			if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
				if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
					log.info("try 冻结接口--个人账户余额不匹配");
					response.setCode(  BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
					response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
					log.info("try 冻结接口。。。。。。{}", response);
					return response;
				}
				if(personalAccount.getAccountBalance().compareTo(new BigDecimal(freezeRequest.getFreezeAmt()))<0){
					response.setCode(  BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getCode());
					response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getMessage());
					log.info("try 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				personalAccount.setAccountBalance(personalAccount.getAccountBalance().subtract(new BigDecimal(freezeRequest.getFreezeAmt())));
				personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				personalAccount.setFreezeBalance(personalAccount.getFreezeBalance().add(new BigDecimal(freezeRequest.getFreezeAmt())));
				personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
			}else{
				log.info("try 发起退款时 冻结资金--个人账户不存在或者被冻结");
				response.setCode( BizResponseCode.PER_ACC_NOTEXIST.getCode());
				response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
				log.info("try 发起退款时 冻结资金 返回参数。。。。。。{}", response);
				return response;
			}			
		}
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		return response;
	}
	/**
	 * 冻结接口接口 confirm 不做任何操作
	 * @param freezeRequest
	 * @return
	 */
	public BaseApiResponse freezeConfirm(FreezeRequest freezeRequest){
		log.info("confirm 冻结接口-请求参数：{}" + EntityUtils.beanToMap(freezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage( BizResponseCode.OK.getMessage());
		log.info("confirm 冻结接口-结束：{}" + response);
		return response;
	}
	
	/**
	 * 冻结接口接口 cancel
	 * 若是商户 
	 * 1.商户账户余额增加，冻结金额扣除
	 * 若是个人
	 * 1.个人账户余额增加，冻结金额扣除
	 * @param freezeRequest
	 * @return
	 */
	public BaseApiResponse freezeCancel(FreezeRequest freezeRequest){
		log.info("cancel 冻结接口-请求参数：{}" + EntityUtils.beanToMap(freezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
			MerchantAccount queryMerchantAccount=new MerchantAccount();
			queryMerchantAccount.setPlatformId(freezeRequest.getPlatformId());
			queryMerchantAccount.setMerchantId(freezeRequest.getMerchantId());
			queryMerchantAccount.setMerchantType(freezeRequest.getMerchantType());
			queryMerchantAccount.setAccountType(freezeRequest.getAccountType());
			MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
			if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
				if(!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())){
					response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
					response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
					log.info("cancel 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				//校验商户账户冻结余额是否充足 和余额加密值的校验
				if(merchantAccount.getFreezeBalance().compareTo(new BigDecimal(freezeRequest.getFreezeAmt()))<0){//商户账户冻结余额不足
					response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
					response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
					log.info("cancel 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				merchantAccount.setAccountBalance(merchantAccount.getAccountBalance().add(new BigDecimal(freezeRequest.getFreezeAmt())));
				merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance().subtract(new BigDecimal(freezeRequest.getFreezeAmt())));
				merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
			}else{
				log.info("cancel 冻结接口--商户账户不存在或者被冻结");
				response.setCode( BizResponseCode.MER_ACC_NOTEXIST.getCode());
				response.setMessage( BizResponseCode.MER_ACC_NOTEXIST.getMessage());
				log.info("cancel 冻结接口 返回参数。。。。。。{}", response);
				return response;
			}
		}
		if(freezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
			PersonalAccount queryPersonalAccount = new PersonalAccount();
			queryPersonalAccount.setPlatformId(freezeRequest.getPlatformId());
			queryPersonalAccount.setPersonalId(freezeRequest.getPersonalId());
			queryPersonalAccount.setAccountType(freezeRequest.getAccountType());
			PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
			if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
				if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
					log.info("cancel 冻结接口--个人账户余额不匹配");
					response.setCode(  BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
					response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
					log.info("cancel 冻结接口。。。。。。{}", response);
					return response;
				}
				if(personalAccount.getFreezeBalance().compareTo(new BigDecimal(freezeRequest.getFreezeAmt()))<0){
					response.setCode( BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getCode());
					response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getMessage());
					log.info("cancel 冻结接口 返回参数。。。。。。{}", response);
					return response;
				}
				personalAccount.setAccountBalance(personalAccount.getAccountBalance().add(new BigDecimal(freezeRequest.getFreezeAmt())));
				personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
				personalAccount.setFreezeBalance(personalAccount.getFreezeBalance().subtract(new BigDecimal(freezeRequest.getFreezeAmt())));
				personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
			}else{
				log.info("cancel 发起退款时 冻结资金--个人账户不存在或者被冻结");
				response.setCode( BizResponseCode.PER_ACC_NOTEXIST.getCode());
				response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
				log.info("cancel 发起退款时 冻结资金 返回参数。。。。。。{}", response);
				return response;
			}
		}
		log.info("cancel 冻结接口-结束：{}" + response);
		return response;
	}
	
	
	/**
	 * 插入冻结流水表 try 正式沉淀数据
	 * @param freezeJournal
	 * @return
	 * @throws BusinessException
	 */
	@Hmily(confirmMethod="insertFreezeJournalConfirm",cancelMethod="insertFreezeJournalCancel")
	public BaseApiResponse insertFreezeJournal(FreezeJournal freezeJournal) throws BusinessException{
		log.info("try插入冻结流水-请求参数：{}" + EntityUtils.beanToMap(freezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		freezeJournal.setTccFlag("1");//生效数据
		int i=freezeJournalMapper.insertSelective(freezeJournal);
		if(i>0){
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("try插入冻结流水-异常：插入数据库报错");
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		log.info("try插入冻结流水-结束：{}" + response);
		return response;
	}
	/**
	 * 插入冻结流水表 confirm 不做任何操作
	 * @param freezeJournal
	 * @return
	 * @throws BusinessException
	 */
	public BaseApiResponse insertFreezeJournalConfirm(FreezeJournal freezeJournal) throws BusinessException{
		log.info("confirm插入冻结流水-请求参数：{}" + EntityUtils.beanToMap(freezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		log.info("confirm插入冻结流水-结束：{}" + response);
		return response;
	}
	/**
	 * 插入冻结流水表 cancel 将流水状态置为失效
	 * @param freezeJournal
	 * @return
	 * @throws BusinessException
	 */
	public BaseApiResponse insertFreezeJournalCancel(FreezeJournal freezeJournal) throws BusinessException{
		log.info("cancel插入冻结流水-请求参数：{}" + EntityUtils.beanToMap(freezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		freezeJournal.setTccFlag("0");//无效数据
		freezeJournal.setUpdateTime(new Date());
		Example example=new Example(FreezeJournal.class);
		Criteria criteria=example.createCriteria();
		criteria.andEqualTo("journalId", freezeJournal.getJournalId());
		int i=freezeJournalMapper.updateByExampleSelective(freezeJournal, example);
		if(i==1){
			response.setCode(  BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("cancel插入冻结流水-异常：修改数据库报错");
			response.setCode(  BizResponseCode.SYSTEM_ERROR.getCode());
			response.setMessage( BizResponseCode.SYSTEM_ERROR.getMessage());
		}
		log.info("cancel插入冻结流水-结束：{}" + response);
		return response;
	}
	/**
	 * 校验冻结流水号是否被解冻
	 * @param unFreezeRequest
	 * @return
	 */
	public BaseApiResponse verificationFreezeId(UnFreezeRequest unFreezeRequest){
		log.info("校验冻结流水号是否被解冻 -- 请求参数：{}"+EntityUtils.beanToMap(unFreezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		UnfreezeJournal queryUnfreezeJournal = new UnfreezeJournal();
		queryUnfreezeJournal.setFreezeId(unFreezeRequest.getFreezeId());
		queryUnfreezeJournal.setOrderNo(unFreezeRequest.getOrderNo());
		UnfreezeJournal unfreezeJournal=unfreezeJournalMapper.selectOne(queryUnfreezeJournal);
		if(null!=unfreezeJournal){
			log.info("校验冻结流水号是否被解冻--冻结资金已解冻");
			response.setCode(BizResponseCode.UNFREEZE_IS_EXIST.getCode());
			response.setMessage(BizResponseCode.UNFREEZE_IS_EXIST.getMessage());
			
		}else{
			log.info("校验冻结流水号是否被解冻--冻结资金未解冻");
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		}
		log.info("校验冻结流水号是否被解冻--结束：{}" + response);
		return response;
	}
	
	
	/**
	 * 解冻接口 调用该方法前需要判断冻结流水号是否被解冻 调用方法 verificationFreezeId
	 * 若是商户
	 * 1.商户账户冻结金额扣除，商户账户余额增加。 
	 * 若是个人
	 * 1.个人账户冻结金额扣除，个人账户余额增加。 
	 */
	@Hmily(confirmMethod="unfreezeConfirm",cancelMethod="unfreezeCancel")
	public BaseApiResponse unfreeze(UnFreezeRequest unFreezeRequest) {
		log.info("try 退款失败时 解冻资金的接口 -请求参数：{}" + EntityUtils.beanToMap(unFreezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		try {
			if(unFreezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())){
				MerchantAccount queryMerchantAccount=new MerchantAccount();
				queryMerchantAccount.setPlatformId(unFreezeRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(unFreezeRequest.getMerchantId());
				queryMerchantAccount.setMerchantType(unFreezeRequest.getMerchantType());
				queryMerchantAccount.setAccountType(unFreezeRequest.getAccountType());
				MerchantAccount merchantAccount= merchantAccountMapper.selectOne(queryMerchantAccount);
				if(null!=merchantAccount&&merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(merchantAccount.getCheckSum())){
						response.setCode( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("try 解冻接口 返回参数。。。。。。{}", response);
						return response;
					}
					//校验商户账户余额是否充足 和余额加密值的校验
					if(merchantAccount.getFreezeBalance().compareTo(new BigDecimal(unFreezeRequest.getUnfreezeAmt()))<0){//商户账户余额不足
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage( BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("try 解冻接口 返回参数。。。。。。{}", response);
						return response;
					}
					merchantAccount.setAccountBalance(merchantAccount.getAccountBalance().add(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					merchantAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
					merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance().subtract(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				}else{
					log.info("try 解冻接口--商户账户不存在或者被冻结");
					response.setCode(BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					log.info("try 解冻接口 返回参数。。。。。。{}", response);
					return response;
				}
			}
			if(unFreezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())){
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(unFreezeRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(unFreezeRequest.getPersonalId());
				queryPersonalAccount.setAccountType(unFreezeRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if(null!=personalAccount&&personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())){
					if(!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8").equals(personalAccount.getCheckSum())){
						log.info("try 解冻接口--个人账户余额不匹配");
						response.setCode( BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("try 解冻接口。。。。。。{}", response);
						return response;
					}
					//校验账户余额是否充足 和余额加密值的校验
					if(personalAccount.getFreezeBalance().compareTo(new BigDecimal(unFreezeRequest.getUnfreezeAmt()))<0){//商户账户余额不足
						response.setCode(BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage( BizResponseCode.PER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("try 解冻接口 返回参数。。。。。。{}", response);
						return response;
					}
					personalAccount.setAccountBalance(personalAccount.getAccountBalance().add(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					personalAccount.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue, "utf-8"));
					personalAccount.setFreezeBalance(personalAccount.getFreezeBalance().subtract(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				}else{
					log.info("try 发起退款时 解冻资金--个人账户不存在或者被冻结");
					response.setCode(BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					log.info("try 发起退款时 解冻资金 返回参数。。。。。。{}", response);
					return response;
				}
				
			}
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			log.info("try 解冻接口 报错--"+e1.getMessage());
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		return response;
		
	}
	
	public BaseApiResponse unfreezeConfirm(UnFreezeRequest unFreezeRequest){
		log.info("confirm 解冻接口-请求参数：{}" + EntityUtils.beanToMap(unFreezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage( BizResponseCode.OK.getMessage());
		log.info("confirm 解冻接口-结束：{}" + response);
		return response;
	}
	/**
	 * 解冻接口回滚
	 * 若是商户
	 * 1.商户账户余额扣除，商户冻结金额增加。 
	 * 若是个人
	 * 1.个人账户余额扣除，个人账户冻结金额增加。 
	 */
	public BaseApiResponse unfreezeCancel(UnFreezeRequest unFreezeRequest){
		log.info("cancel 解冻接口-请求参数：{}" + EntityUtils.beanToMap(unFreezeRequest));
		BaseApiResponse response = new BaseApiResponse();
		try {
			if (unFreezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_MERCHANT.getCode())) {
				MerchantAccount queryMerchantAccount = new MerchantAccount();
				queryMerchantAccount.setPlatformId(unFreezeRequest.getPlatformId());
				queryMerchantAccount.setMerchantId(unFreezeRequest.getMerchantId());
				queryMerchantAccount.setMerchantType(unFreezeRequest.getMerchantType());
				queryMerchantAccount.setAccountType(unFreezeRequest.getAccountType());
				MerchantAccount merchantAccount = merchantAccountMapper.selectOne(queryMerchantAccount);
				if (null != merchantAccount && merchantAccount.getStatus().equals(AccountStateEnums.normal.getCode())) {
					if (!MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()), balanceSaltValue,
							"utf-8").equals(merchantAccount.getCheckSum())) {
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("cancel 解冻接口 返回参数。。。。。。{}", response);
						return response;
					}
					// 校验商户账户余额是否充足 和余额加密值的校验
					if (merchantAccount.getAccountBalance()
							.compareTo(new BigDecimal(unFreezeRequest.getUnfreezeAmt())) < 0) {// 商户账户余额不足
						response.setCode(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getCode());
						response.setMessage(BizResponseCode.MER_ACC_BALANCE_NOT_ENOUGH.getMessage());
						log.info("cancel 解冻接口 返回参数。。。。。。{}", response);
						return response;
					}
					merchantAccount.setAccountBalance(
							merchantAccount.getAccountBalance().subtract(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					merchantAccount
							.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(merchantAccount.getAccountBalance()),
									balanceSaltValue, "utf-8"));
					merchantAccount.setFreezeBalance(merchantAccount.getFreezeBalance()
							.add(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					merchantAccountMapper.updateByPrimaryKeySelective(merchantAccount);
				} else {
					log.info("cancel 解冻接口--商户账户不存在或者被冻结");
					response.setCode(BizResponseCode.MER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.MER_ACC_NOTEXIST.getMessage());
					log.info("cancel 解冻接口 返回参数。。。。。。{}", response);
					return response;
				}
			}
			if (unFreezeRequest.getCustomerType().equals(CustomerTypeEnums.CUST_PERSONAL.getCode())) {
				PersonalAccount queryPersonalAccount = new PersonalAccount();
				queryPersonalAccount.setPlatformId(unFreezeRequest.getPlatformId());
				queryPersonalAccount.setPersonalId(unFreezeRequest.getPersonalId());
				queryPersonalAccount.setAccountType(unFreezeRequest.getAccountType());
				PersonalAccount personalAccount = personalAccountMapper.selectOne(queryPersonalAccount);
				if (null != personalAccount && personalAccount.getStatus().equals(AccountStateEnums.normal.getCode())) {
					if (!MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()), balanceSaltValue,
							"utf-8").equals(personalAccount.getCheckSum())) {
						log.info("cancel 解冻接口--个人账户余额不匹配");
						response.setCode(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getCode());
						response.setMessage(BizResponseCode.PER_ACC_BALANCE_NOT_MATE.getMessage());
						log.info("cancel 解冻接口。。。。。。{}", response);
						return response;
					}

					personalAccount.setAccountBalance(
							personalAccount.getAccountBalance().subtract(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					personalAccount
							.setCheckSum(MD5.sign(CurrencyUtils.decimaltoString(personalAccount.getAccountBalance()),
									balanceSaltValue, "utf-8"));
					personalAccount.setFreezeBalance(personalAccount.getFreezeBalance()
							.add(new BigDecimal(unFreezeRequest.getUnfreezeAmt())));
					personalAccountMapper.updateByPrimaryKeySelective(personalAccount);
				} else {
					log.info("cancel 解冻 发起退款时 冻结资金--个人账户不存在或者被冻结");
					response.setCode(BizResponseCode.PER_ACC_NOTEXIST.getCode());
					response.setMessage(BizResponseCode.PER_ACC_NOTEXIST.getMessage());
					log.info("cancel 解冻 发起退款时 冻结资金 返回参数。。。。。。{}", response);
					return response;
				}

			}
			response.setCode(BizResponseCode.OK.getCode());
			response.setMessage(BizResponseCode.OK.getMessage());
		} catch (Exception e1) {
			e1.printStackTrace();
			log.info("cancel 解冻接口 报错--" + e1.getMessage());
			response.setCode(BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage(BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
			return response;
		}
		log.info("cancel 解冻接口-结束：{}" + response);
		return response;
	}
	
	@Hmily(confirmMethod="insertUnfreezeJournalConfirm",cancelMethod="insertUnfreezeJournalCancel")
	public BaseApiResponse insertUnfreezeJournal(UnfreezeJournal unfreezeJournal){
		log.info("try插入解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		unfreezeJournal.setTccFlag("1");//生效数据
		int i=unfreezeJournalMapper.insertSelective(unfreezeJournal);
		if(i>0){
			response.setCode( BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("try插入解冻流水-异常：插入数据库报错--{}" + EntityUtils.beanToMap(unfreezeJournal));
			response.setCode( BizResponseCode.SERVICE_NOT_AVALIABLE.getCode());
			response.setMessage( BizResponseCode.SERVICE_NOT_AVALIABLE.getMessage());
		}
		log.info("try插入解冻流水-结束：{}" + response);
		return response;
	}
	
	/**
	 * 插入解冻流水表 confirm 不做任何操作
	 * @param unfreezeJournal
	 * @return
	 */
	public BaseApiResponse insertUnfreezeJournalConfirm(UnfreezeJournal unfreezeJournal) {
		log.info("confirm插入解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		response.setCode( BizResponseCode.OK.getCode());
		response.setMessage(BizResponseCode.OK.getMessage());
		log.info("confirm插入解冻流水-结束：{}" + response);
		return response;
	}
	/**
	 * 插入解冻流水表 cancel 将流水状态置为失效
	 * @param unfreezeJournal
	 * @return
	 */
	public BaseApiResponse insertUnfreezeJournalCancel(UnfreezeJournal unfreezeJournal){
		log.info("cancel插入解冻流水-请求参数：{}" + EntityUtils.beanToMap(unfreezeJournal));
		BaseApiResponse response = new BaseApiResponse();
		unfreezeJournal.setTccFlag("0");//无效数据
		unfreezeJournal.setUpdateTime(new Date());
		int i=unfreezeJournalMapper.updateByPrimaryKeySelective(unfreezeJournal);
		if(i==1){
			response.setCode(  BizResponseCode.OK.getCode());
			response.setMessage( BizResponseCode.OK.getMessage());
		}else{
			log.info("cancel插入解冻流水-异常：修改数据库报错--{}"+ EntityUtils.beanToMap(unfreezeJournal));
			response.setCode(  BizResponseCode.SYSTEM_ERROR.getCode());
			response.setMessage( BizResponseCode.SYSTEM_ERROR.getMessage());
		}
		log.info("cancel插入解冻流水-结束：{}" + response);
		return response;
	}
}
